package com.quzi.kettle.activitise.main;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.httprequest.AsyncBean;
import com.example.httprequest.AsyncHttpUtilClient;
import com.loopj.android.http.RequestParams;
import com.quzi.kettle.MyApplication;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.api.response.BooleanResponse;
import com.quzi.kettle.utils.MSharePerference;
import com.quzi.kettle.utils.StrPattern;
import com.quzi.kettle.view.TitleBarView;

public class UpdatePswActivity extends BaseActivity {

	private TitleBarView titleView;
	private EditText curr_psw, new_psw, confirm_psw;
	private Button confirm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_changepsw);

		initTitleBar();
		curr_psw = (EditText) findViewById(R.id.curr_psw);
		new_psw = (EditText) findViewById(R.id.new_psw);
		confirm_psw = (EditText) findViewById(R.id.confirm_psw);
		confirm = (Button) findViewById(R.id.confirm);
		confirm.setOnClickListener(this);
	}

	@Override
	protected void initTitleBar() {
		// TODO Auto-generated method stub
		super.initTitleBar();
		titleView = (TitleBarView) findViewById(R.id.titlebar);
		// titleView.setLeftIconVisible(false);
		titleView.setTitle(getResources().getString(R.string.change_psw));
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.confirm:
			// 点击确认
			if (checkEt()) {
				// 校验成功

			} else {

			}
			break;
		}
	}

	private boolean checkEt() {
		// TODO Auto-generated method stub
		String currPsw = curr_psw.getText().toString();
		String newPsw = new_psw.getText().toString();
		String confirmPsw = confirm_psw.getText().toString();
		if (currPsw.isEmpty() || !StrPattern.checkpswd(currPsw)) {
			showTextToast("密码应为6-20位数字字母组合");
			return false;
		}
		if (newPsw.isEmpty() || !StrPattern.checkpswd(newPsw)) {
			showTextToast("新密码应为6-20位数字字母组合");
			return false;
		}
		if (confirmPsw.isEmpty() || !StrPattern.checkpswd(confirmPsw)) {
			showTextToast("确认密码应为6-20位数字字母组合");
			return false;
		}
		if (currPsw.equals(MSharePerference
				.getInstance(getApplicationContext()).get("password"))) {

			if (newPsw.equals(confirmPsw)) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("password", newPsw);
				AsyncHttpUtilClient
						.request(
								getApplicationContext(),
								new AsyncBean(
										AsyncHttpUtilClient.POST,
										MyApplication.url
												+ MyApplication.requestMap
														.get(MyApplication.USER_UPDATEPASSWORD),
										MyApplication.header,
										new RequestParams(map)),
								new BooleanResponse<Boolean>() {

									@Override
									public void onSuccess(Boolean t) {
										// TODO Auto-generated method stub
										if (t) {
											UIHandler.obtainMessage(TOASTMSG,
													"修改密码成功").sendToTarget();
											finish();
										} else {
											UIHandler.obtainMessage(TOASTMSG,
													"修改密码失败").sendToTarget();
										}
									}

									@Override
									public void onFailure(int arg0, String str) {
										// TODO Auto-generated method stub
										UIHandler.obtainMessage(TOASTMSG, str)
												.sendToTarget();
									}
								});

			} else {
				UIHandler.obtainMessage(TOASTMSG, "输入新密码不一致，请重新输入")
						.sendToTarget();
			}

		} else {
			UIHandler.obtainMessage(TOASTMSG, "输入密码错误").sendToTarget();
		}
		return false;
	}

}
